<template>
  <div class="advert">
    <web-view :src="url1" @message="bindmessage" v-if="showWeb"></web-view>
  </div>
</template>

<script>
import { setTimeout } from 'timers';

export default {
  data () {
    return {
      baseUrl:'',
      url1:'',
       the_url:"",
      flag: true,
      golden:'',//h5传来的参数
      score:'',
      showWeb:true,//刷新页面
      gameId:'',
      userId:'',
      friendId:''//朋友的id
    }
  },

  components: {

  },
  mounted()
  {
   
   
	 
  },

  methods: {
    //接收h5发送的数据
    bindmessage(e){
      this.userId=e.target.data[0].userId;//本人的id
      // this.$currency.showToast(this.userId)
    },

    getPath(worthPath){
      let pathNames = worthPath.split("\\");
      return "/"+pathNames[1] + "/" + pathNames[2] + "/main";
    },
    async getInitUrl(){
      let res=await this.$request.getData('/game/getInitUrl')
      if(res.data.level=='success'){
        this.url1=res.data.data
      }
    },
  },
  created(){
    this.getInitUrl()
     
  },
  async onLoad(options)
  {
    // this.$currency.showToast(`我是id${options.friendId}`)
    console.log(options,888)
    if(options.friendId){
      //初次进入保存分享链接的朋友id
      wx.setStorageSync('friendId',options.friendId)
      // this.friendId=options.friendId
    }else{
      // this.friendId=''
    }
    let path = this.getPath(this._self.$options["__file"]);
    wx.setStorageSync('toPage',path);
    if(this.url1 == ""){
      let res=await this.$request.getData('/game/getInitUrl')
      if(res.data.level=='success'){
        this.url1=res.data.data
      }
      console.log(this.url1,"a")
    }

    console.log(this.url1,"b")
    let userKey = wx.getStorageSync("user_key");
    let userToken=wx.getStorageSync('userToken');
    let friendId=wx.getStorageSync('friendId');
    // this.$currency.showToast(`我是id!!!!!!${friendId}`,'none',3000)
    if(userToken == '' || userKey == ""){
      wx.navigateTo({

        url:"/pages/login/main"
      })
    }

    //  flag判断userKey已经赋值过到url1上了
    if(userKey != "" && this.flag)
    {
      this.flag = !this.flag;
    if(friendId){
      this.url1 = this.url1 + "?user_key=" + userKey+"&userToken="+userToken+'&friendId='+friendId;
    }else{
      this.url1 = this.url1 + "?user_key=" + userKey+"&userToken="+userToken
    }
    // console.log(this.url1)
    // setTimeout(()=>{
    //   this.$currency.showToast(this.url1,'none',3000)
    // },8000)
      

      
    }
  },
  onHide(){
      this.the_url="",
      this.flag= true,
      this.golden='',//h5传来的参数
      this.score='',
      // this.showWeb=true,//刷新页面
      this.gameId=''
      this.url1=''
  },
  async onShow()
  {

    // 对实例进行设置
    this.back = wx.getBackgroundAudioManager() 
    this.back.src = "http://file.rzkeji.com/web/f6/game/back_music.mp3"
    this.back.title = 'Tassel'   // 标题为必选项
    this.back.play()  
    // if(this.url1 == ""){
    //   let res=await this.$request.getData('/game/getInitUrl')
    //   if(res.data.level=='success'){
    //     this.url1=res.data.data
    //   }
    //   console.log(this.url1,"a")
    // }

    // console.log(this.url1,"b")
    // let userKey = wx.getStorageSync("user_key");
    // let userToken=wx.getStorageSync('userToken');
    // let friendId=wx.getStorageSync('friendId');
    // if(userToken == '' || userKey == ""){
    //   wx.navigateTo({

    //     url:"/pages/login/main"
    //   })
    // }

    // //  flag判断userKey已经赋值过到url1上了
    // if(userKey != "" && this.flag)
    // {
    //   this.flag = !this.flag;
    // if(friendId){
    //   this.url1 = this.url1 + "?user_key=" + userKey+"&userToken="+userToken+'&friendId='+friendId;
    // }else{
    //   this.url1 = this.url1 + "?user_key=" + userKey+"&userToken="+userToken
    // }
    // console.log(this.url1)
    // setTimeout(()=>{
    //   this.$currency.showToast(this.url1,'none',3000)
    // },5000)
      

      
    // }
    // wx.setStorageSync('gameId','')//进入游戏页，清除id
    // console.log(this.url1,888)

  },


 onShareAppMessage(options) {
   const _this=this
  if(this.userId){
    var url='/pages/contentHome/main'+'?friendId='+this.userId
  }else{
    var url='/pages/contentHome/main'
  }
  let userToken=wx.getStorageSync('userToken')
 const res= this.$request.postData('/game/relive',{
    user_token:userToken,
    type:'share_relive',
  }).then(res=>{
     if(res.data.level=='success'){
    // _this.showWeb=false
    
    // setTimeout(()=>{
    //   _this.showWeb=true
    // },3000)
    setTimeout(()=>{
      wx.navigateTo({url:'/pages/contentHome/main'})
    },1000)
  }else{
    _this.$currency.showToast(res.data.message,'none',6000)
  }
  })
 
  console.log(url,'分享地址')
    return {
      title: '全民拉金条',
      desc:'一起来拉金条',
      path: url
    }
  },



   




}
</script>

<style scoped lang="scss" type="text/css">

</style>
